package com.app.controller;


import com.app.pojo.Item;
import com.app.pojo.Result;
import com.app.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@CrossOrigin
public class ItemController {
    @Autowired
    ItemService itemService;

    @PostMapping("/item")
    public Result add(@RequestBody Item item){
        log.info("上架商品");
        itemService.add(item);
        return Result.success();
    }

    @DeleteMapping("/item/{item_id}")
    public Result delete(@PathVariable int item_id){
        log.info("下架商品");
        itemService.delete(item_id);
        return Result.success();
    }


    @PutMapping("/item")
    public Result update(@RequestBody Item item){
        log.info("修改商品信息");
        itemService.update(item);
        return Result.success();
    }


    @GetMapping("/item/{item_id}")
    public Result List(@PathVariable int item_id){
        log.info("根据有商品ID：{}，查询商品",item_id);
        List<Item> itemList = itemService.list(item_id);
        if(itemList.isEmpty()){
            return Result.error("该商品不存在");
        }else{
            return Result.success(itemList);
        }
    }

    //搜索所有商品
    @GetMapping("/item/selectAll")
    public Result selectAll(){
        List<Item> itemList = itemService.selectAll();
        if(!itemList.isEmpty()){
            return Result.success(itemList);
        }else{
            return  Result.error("搜索失败");
        }
    }
}